import java.util.*;


public class Main {
    public static Integer solution(Integer n, Integer m, String s, String c) {
        Map<Character, Integer> kindsNum = new HashMap<>();
        Map<Character, Integer> kindsUser = new HashMap<>();

        Integer buyNumbers = 0;
        char[] sArray = s.toCharArray();
        char[] cArray = c.toCharArray();

        for(int i = 0; i < sArray.length; i++) {
            kindsNum.put(sArray[i], kindsNum.getOrDefault(sArray[i], 0) + 1);
        }

        for(int j = 0; j < cArray.length; j++) {
            kindsUser.put(cArray[j], kindsUser.getOrDefault(cArray[j], 0) + 1);
        }

        for (char k = 'a'; k < 'z'; k++) {
            if (kindsNum.keySet().contains(k) && kindsUser.keySet().contains(k)) {
                buyNumbers = buyNumbers + Math.min(kindsUser.get(k), kindsNum.get(k));
            }
        }

        return buyNumbers;

    }


    public static void main(String[] args) {
        System.out.println(solution(3, 4, "abc", "abcd") == 3 );
        System.out.println(solution(4, 2, "abbc", "bb") == 2 );
        System.out.println(solution(5, 4, "bcdea", "abcd") == 4 );

    }

}

